import requests
import re
import optparse
from bs4 import BeautifulSoup

""" 
用ip反查绑定过的域名
需要安装 bs4 lxml re requests optparse库
使用: ip反查域名.py -i ip地址
 """

def main():
    usage = 'Usage:%prog -i <ip>'
    parser = optparse.OptionParser(usage)
    parser.add_option('-i',dest='IP',type='string',help='enter target ip')
    options,args = parser.parse_args()
    ip = options.IP

    fake_headrs = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2952.99 Safari/537.36',
    'Accept': '*/*'
    }
    target_url = 'https://site.ip138.com/'+ip
    reponse = requests.get(target_url,headers=fake_headrs)
    # print(reponse.text)
    pattern = re.compile('<li><span class="date".*?</li>',re.S)
    conntent = re.findall(pattern,reponse.text)
    # print(conntent)
    for i in conntent:
        soup = BeautifulSoup(i,'lxml')
        url = soup.a.attrs['href']
        print(url.strip('/'))

if __name__ == '__main__':
    main()
